package nl.windgazer.arachne.internal.transform;

import nl.windgazer.arachne.annotations.IncludeFontFace;

import org.apache.tapestry5.Asset;
import org.apache.tapestry5.func.Worker;
import org.apache.tapestry5.ioc.services.SymbolSource;
import org.apache.tapestry5.model.MutableComponentModel;
import org.apache.tapestry5.plastic.PlasticClass;
import org.apache.tapestry5.services.AssetSource;
import org.apache.tapestry5.services.ClassTransformation;
import org.apache.tapestry5.services.TransformMethod;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
import org.apache.tapestry5.services.transform.TransformationSupport;

/**
 * Easy way of adding a font-squirrel packaged fonts to the page using an annotation.
 * Comes with a set of pre-packed fonts for easy usage :)
 * @author mreuring
 *
 */
public class IncludeFontFaceWorker extends IncludeAbstractWorker<IncludeFontFace> {

	private final static String BASEPACKAGE = "classpath:com/fontsquirrel/fontfacekits/";
	private final static String BASEIDENTIFIER = "-fontfacekit/stylesheet.css";

    private final Worker<Asset> importStylesheet = new Worker<Asset>()
    {
        public void work(Asset asset)
        {
            jss.importStylesheet(asset);
        };
    };

	private JavaScriptSupport jss;

	public IncludeFontFaceWorker(JavaScriptSupport jss, SymbolSource symbolSource, AssetSource assetSource) {
		super(symbolSource, assetSource);
		this.jss = jss;
	}

	@Override
    protected void decorateMethod(ClassTransformation transformation, MutableComponentModel model,
            TransformMethod method, IncludeFontFace annotation)
    {
        importStylesheets(transformation, model, method, annotation.value());
    }

	@Override
	protected Class<IncludeFontFace> getAnnotationClass() {
		return IncludeFontFace.class;
	}

	@Override
    protected String[] expandPaths(String[] paths)
    {
        String[] result = new String[paths.length];

        for (int i = 0; i < paths.length; i++) {
			StringBuffer sb = new StringBuffer(BASEPACKAGE)
				.append(paths[i])
				.append(BASEIDENTIFIER);
			result[i] = sb.toString();
        }

        return super.expandPaths(result);
    }

    private void importStylesheets(ClassTransformation transformation, MutableComponentModel model,
            TransformMethod method, String[] paths)
    {
        decorateMethodWithOperation(transformation, model, method, paths, importStylesheet);
    }

	public void transform(PlasticClass arg0, TransformationSupport arg1,
			MutableComponentModel arg2) {
		// TODO Auto-generated method stub
		
	}

}
